#include "freertos_demo.h"

#include "FreeRTOS.h"
#include "task.h"

#include "LED.h"
#include "Key.h"

#define START_TASK_STACK 128
#define START_TASK_PRIORITY 1
TaskHandle_t start_task_handle;
void start_task(void *  pvParameters);

#define TASK1_STACK 128
#define TASK1_PRIORITY 2
TaskHandle_t task1_handle;
void task1(void *  pvParameters);

#define TASK2_STACK 128
#define TASK2_PRIORITY 3
TaskHandle_t task2_handle;
void task2(void *  pvParameters);

#define TASK3_STACK 128
#define TASK3_PRIORITY 4
TaskHandle_t task3_handle;
void task3(void *  pvParameters);

void freertos_start(void)
{
    /*1.创建一个启动任务*/
    xTaskCreate((TaskFunction_t) start_task,
                            (char *)  "start_task", 
                            (configSTACK_DEPTH_TYPE) START_TASK_STACK,
                            (void *) NULL,
                            (UBaseType_t) START_TASK_PRIORITY,
                            (TaskHandle_t *)  &start_task_handle );
    /*2.启动调度器:会自动创建空闲任务*/
    vTaskStartScheduler();
}


/*启动任务:用来创建其他Task*/
void start_task(void *  pvParameters)
{
    /*进入临界区:里面的代码不会被打断*/
    taskENTER_CRITICAL();

    xTaskCreate((TaskFunction_t) task1,
                            (char *)  "task1", 
                            (configSTACK_DEPTH_TYPE) TASK1_STACK,
                            (void *) NULL,
                            (UBaseType_t) TASK1_PRIORITY,
                            (TaskHandle_t *)  &task1_handle );

    xTaskCreate((TaskFunction_t) task2,
                            (char *)  "task2", 
                            (configSTACK_DEPTH_TYPE) TASK2_STACK,
                            (void *) NULL,
                            (UBaseType_t) TASK2_PRIORITY,
                            (TaskHandle_t *)  &task2_handle );

    xTaskCreate((TaskFunction_t) task3,
                            (char *)  "task3", 
                            (configSTACK_DEPTH_TYPE) TASK3_STACK,
                            (void *) NULL,
                            (UBaseType_t) TASK3_PRIORITY,
                            (TaskHandle_t *)  &task3_handle ); 
    
    //删除自身,启动任务只需要执行一次即可                         
    vTaskDelete(NULL);

    /*退出临界区*/
    taskEXIT_CRITICAL();
}


void task1(void *  pvParameters)
{
    while (1)
    {
        printf("task1运行...\r\n");
        LED_Toggle(LED1_Pin);
        vTaskDelay(500);
    }
}


void task2(void *  pvParameters)
{
    while (1)
    {
        printf("task2运行...\r\n");
        LED_Toggle(LED2_Pin);
        vTaskDelay(500);
    }
}


char task_info[500];

void task3(void *  pvParameters)
{
    uint8_t key = 0;
    while (1)
    {
        printf("task3运行...\r\n");
        key = Key_Detect();
        if (key == KEY1_PRESS)
        {
            printf("挂起task1\r\n");
            vTaskSuspend(task1_handle);                  
        }
        else if (key == KEY2_PRESS)
        {
            printf("恢复task1\r\n");
            vTaskResume(task1_handle);
        }
        else if (key == KEY3_PRESS)
        {
            printf("挂起调度器\r\n");
            vTaskSuspendAll();
        }
        else if (key == KEY4_PRESS)
        {
            printf("恢复调度器\r\n");
            xTaskResumeAll();
        }

        vTaskList(task_info);
        printf("%s\r\n",task_info);
                
        vTaskDelay(500);        
    }
}

